package com.tsxy.mall.Intercepter;


import com.tsxy.mall.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginIntercepter extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String contextPath = request.getContextPath();
        System.err.println(contextPath);
        session.setAttribute("path", contextPath);
        User user = (User) session.getAttribute("user");
        if (user == null){
            response.sendRedirect("/user/toLogin");
            return false;
        }else {
            return true;
        }

    }
}
